package com.base.pig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.base.pig.dto.ScAnnotationDto;
import com.base.pig.dto.ScAnnotationListDto;
import com.base.pig.entity.ScAnnotation;
import com.base.pig.entity.ScAnnotationType;
import com.base.pig.mapper.ScAnnotationMapper;
import com.base.pig.mapper.ScAnnotationTypeMapper;
import com.base.pig.util.R;
import com.base.pig.service.ScAnnotationService;
import com.base.pig.util.SecurityUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName intelligent-city
 * @Package com.pig4cloud.pig.lab.annotation.service.impl
 * @ClassName ScAnnotationServiceImpl
 * @Description 标注接口实现类
 * @Company lab
 * @Author zgq
 * @Date 2020/7/13 19:11
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */

@Service
@AllArgsConstructor
public class ScAnnotationServiceImpl extends ServiceImpl<ScAnnotationMapper, ScAnnotation> implements ScAnnotationService {

    private ScAnnotationMapper scAnnotationMapper;

    private ScAnnotationTypeMapper scAnnotationTypeMapper;

    @Override
    public R selectAll() {
        List<ScAnnotationListDto> scAnnotationListDtoList = new ArrayList<>();
        List<ScAnnotation> scAnnotationAllList = scAnnotationMapper.selectList(new QueryWrapper<ScAnnotation>().eq("user_id", SecurityUtils.userid).orderByAsc("gmt_create", "us_id", "type"));
        List<ScAnnotationType> parentList = scAnnotationTypeMapper.selectList(new QueryWrapper<ScAnnotationType>().eq("pid", 0));
        List<ScAnnotationType> childList = scAnnotationTypeMapper.selectList(new QueryWrapper<ScAnnotationType>().ne("pid", 0));
        List<Map<String,Object>>     reList=new ArrayList();
        Map<String,Object>  remap=new HashMap();
        for (int i = 0; i < parentList.size(); i++) {
            ScAnnotationType parent = parentList.get(i);
            List<ScAnnotationType> childScAnnotationTypeList = new ArrayList<>();
            for (int k = 0; k < childList.size(); k++) {
                ScAnnotationType child = childList.get(k);
                if (child.getPid() == parent.getId()) {
                    childScAnnotationTypeList.add(child);
                    childList.remove(k);
                    k--;
                }
            }
            parent.setChildScAnnotationTypeList(childScAnnotationTypeList);
            parentList.set(i, parent);
        }

        int seq = 0;
        parentList.forEach(parent -> {
            ScAnnotationListDto scAnnotationListDto = new ScAnnotationListDto();
            scAnnotationListDto.setId(parent.getName() + parent.getId());
            scAnnotationListDto.setType(parent.getId());
            scAnnotationListDto.setTypeName(parent.getName());
            //mag 20220223
            scAnnotationListDto.setTitle(parent.getName());
            scAnnotationListDto.setLabel(parent.getName());
            scAnnotationListDto.setName(parent.getName());
            scAnnotationListDto.setSeq(parent.getId());
            scAnnotationListDto.setKey(parent.getId());



            List<ScAnnotationDto> scAnnotationDtoList = new ArrayList<>();
            List<ScAnnotationType> childScAnnotationTypeList = parent.getChildScAnnotationTypeList();
            for (int i = 0; i < scAnnotationAllList.size(); i++) {
                ScAnnotation scAnnotation = scAnnotationAllList.get(i);
                for (int k = 0; k < childScAnnotationTypeList.size(); k++) {
                    ScAnnotationType childScAnnotationType = childScAnnotationTypeList.get(k);
                    if (scAnnotation.getType() == childScAnnotationType.getId()) {
                        Map<String, String> line = new HashMap<>();
                        line.put("sx", scAnnotation.getSx());
                        line.put("sy", scAnnotation.getSy());
                        line.put("sh", scAnnotation.getSh());
                        line.put("ex", scAnnotation.getEx());
                        line.put("ey", scAnnotation.getEy());
                        line.put("eh", scAnnotation.getEh());

                        String  title="";
                        if(scAnnotation.getUsId()!=null  &&   !"".equals(scAnnotation.getUsId())){
                            title= childScAnnotationType.getName()+"("+scAnnotation.getUsId()+")";
                        }else{
                            title= childScAnnotationType.getName();
                        }
                        ScAnnotationDto scAnnotationDto = new ScAnnotationDto(scAnnotation.getId(), scAnnotation.getXzb(), scAnnotation.getYzb(), scAnnotation.getHzb(), scAnnotation.getType(), childScAnnotationType.getName(), scAnnotation.getState(), scAnnotation.getUsId(), scAnnotation.getData(), line, title, title, title,scAnnotation.getId(),scAnnotation.getId());
                        scAnnotationDtoList.add(scAnnotationDto);
                        scAnnotationAllList.remove(i);
                        i--;
                    }
                }
            }
            //mag  20220223
            //scAnnotationListDto.setDataList(scAnnotationDtoList);
            scAnnotationListDto.setChildren(scAnnotationDtoList);
            if (!scAnnotationDtoList.isEmpty()) {
                scAnnotationListDtoList.add(scAnnotationListDto);
            }


            remap.put("id",0);
            remap.put("key",0);
            remap.put("title","标注数据");
            remap.put("name","标注数据");
            remap.put("label","标注数据");
            remap.put("children",scAnnotationListDtoList);

        });

        reList.add(remap);
        return R.ok(reList);
    }

    @Override
    public R delete(List<Long> ids) {
        scAnnotationMapper.deleteBatchIds(ids);
        return R.ok();
    }

    @Override
    public R insertList(List<ScAnnotationDto> ScAnnotationDtoList) {
        List<ScAnnotation> scAnnotationList = new ArrayList<>();
        ScAnnotationDtoList.forEach(scAnnotationDto -> {
            ScAnnotation scAnnotation = new ScAnnotation();
            scAnnotation.setUserId(SecurityUtils.userid);
            scAnnotation.setXzb(scAnnotationDto.getXzb());
            scAnnotation.setYzb(scAnnotationDto.getYzb());
            scAnnotation.setHzb(scAnnotationDto.getHzb());
            scAnnotation.setType(scAnnotationDto.getType());
            scAnnotation.setState(scAnnotationDto.getState());
            scAnnotation.setUsId(scAnnotationDto.getUsId());
            scAnnotation.setData(scAnnotationDto.getData());
            Map<String, String> line = scAnnotationDto.getLine();
            if (line != null) {
                scAnnotation.setSx(line.get("sx"));
                scAnnotation.setSy(line.get("sy"));
                scAnnotation.setSh(line.get("sh"));
                scAnnotation.setEx(line.get("ex"));
                scAnnotation.setEy(line.get("ey"));
                scAnnotation.setEh(line.get("eh"));
            }
            scAnnotationList.add(scAnnotation);
        });
        saveBatch(scAnnotationList);
        List result = new ArrayList();
        scAnnotationList.forEach(scAnnotation -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", scAnnotation.getId());
            map.put("type", scAnnotation.getType());
            result.add(map);
        });
        return R.ok(result);
    }

    @Override
    public R updateStateByIds(List<Long> ids, int state) {
        scAnnotationMapper.updateStateByIds(ids, state);
        return R.ok();
    }
}
